<?php

namespace app\avikcm\controller;

use app\avikcm\model\NewsCategory as ModelNewsCategory;
use clt\Leftnav;
use think\Db;
use think\facade\Request;
use think\facade\Env;

class NewsCategory extends Common
{
    function initialize()
    {
        parent::initialize();
    }
    public function index()
    {
        if (request()->isPost()) {
            $arr = Db::name('news_category')->where('del', 0)->order('sort desc')->select();
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $arr, 'is' => true, 'tip' => '操作成功'];
        }
        return view();
    }
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['create_time'] = date('Y-m-d H:i:s');
            if ($data['pid'] == 0) {
                return $result = ['code' => 0, 'msg' => '请选择上级分类!'];
            }
            db('news_category')->insert($data);
            return $result = ['code' => 1, 'msg' => '添加成功!', 'url' => url('index')];
        }
        // $list = db('news_category')->where(['del' => 0, 'pid' => 0])->select();
        // $this->assign('list', $list);
        $nav = new Leftnav();
        $list = ModelNewsCategory::all(function ($query) {
            $query->where('del',0)->order('sort', 'desc');
        });
        $arr = $nav->menu($list);
        $this->assign('list', $arr);
        return $this->fetch();
    }
    public function order()
    {
        $db = db('news_category');
        $data = input('post.');
        if ($db->update($data) !== false) {
            return $result = ['code' => 1, 'msg' => '排序更新成功!', 'url' => url('index')];
        } else {
            return $result = ['code' => 0, 'msg' => '排序更新失败!'];
        }
    }

    public function del()
    {
        $id = input('post.id');
        $exists = Db::name('news_category')->where('pid', $id)->where('del', 0)->find();
        if ($exists) {
            return ['code' => 0, 'msg' => '此分类下有正在使用的子分类，无法删除!'];
        } else {
            $this->delauth($id);
            return $result = ['code' => 1, 'msg' => '删除成功!'];
        }
    }
    public function delauth($id)
    {
        db('news_category')->where('id', $id)->update(['del' => 1]);
        $authSon = Db::name('news_category')->where('pid', $id)->select();
        if (!empty($authSon)) {
            foreach ($authSon as $k => $v) {
                db('news_category')->where('id', $v['id'])->update(['del' => 1]);
            }
        }
        return true;
    }

    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $res = db('news_category')->where('id', $data['id'])->update($data);
            if ($res) {
                return json(['code' => 1, 'msg' => '保存成功!', 'url' => url('index')]);
            } else {
                return json(['code' => 0, 'msg' => '保存失败！']);
            }
        } else {
            $id = input('id');
            $info = db('news_category')->where('id', $id)->find();
            $nav = new Leftnav();
            $list = ModelNewsCategory::all(function ($query) {
                    $query->where('del', 0)->order('sort', 'desc');
                });
            $arr = $nav->menu($list);
            $this->assign('list', $arr);
            $this->assign('info', $info);
            return $this->fetch('add');
        }
    }
    //设置展示
    public function setOpen()
    {
        $id = input('post.id');
        $is_show = input('post.is_show');
        if (db('news_category')->where('id=' . $id)->update(['is_show' => $is_show]) !== false) {
            return ['status' => 1, 'msg' => '设置成功!'];
        } else {
            return ['status' => 0, 'msg' => '设置失败!'];
        }
    }

    public function getCategoryByBrand()
    {
        if (request()->isPost()) {
            $brand_id = input('brand_id');
            $arr = Db::name('news_category')->where('del', 0)->where('pid', $brand_id)->order('sort desc')->select();
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $arr, 'is' => true, 'tip' => '操作成功'];
        }
        return view();
    }

    public function getSubCategory()
    {
        if (request()->isPost()) {
            $brand_id = input('brand_id');
            $arr = Db::name('news_category')->where('del', 0)->where('pid', $brand_id)->order('sort desc')->select();
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $arr, 'is' => true, 'tip' => '操作成功'];
        }
        return view();
    }
}
